#encoding:utf-8   
from Write_data import*
import spider_weather
import pandas as pd
import spider_valley
import re
import numpy as np

class SpiderModular():

    def __init__(self,outputFilename="",place=0):

        #数据成员
        self.outputData_jiu=pd.DataFrame()#最终数据dataframe类型
        self.outputData_si=pd.DataFrame()#最终数据dataframe类型
        self.outputFilename = outputFilename#保存到文件名
        self.place= place
        self.valleyData_jiu = pd.DataFrame()
        self.valleyData_si = pd.DataFrame()
        self.weatherData_jiu = pd.DataFrame()
        self.weatherData_si = pd.DataFrame()

        #函数
        self.main()

    def main(self):

        #数据初始化
        self.initData()

        #利用数据库左外连接进行数据合并
        self.mergeData()


    #封装
    #数据初始化
    def initData(self):
        if self.place == 0:
            #700-2180
            self.valleyData_jiu=spider_valley.valley_spider(place=0,dataAmount=2180,fileName="九寨沟客流.csv").valleyData#700
        
            #读取天气数据
            self.weatherData_jiu=spider_weather.Weather_spider(place=0,fileName="九寨沟天气.csv").weatherData
        
        elif self.place==1:
            #300-1820
            self.valleyData_si=spider_valley.valley_spider(place=1,dataAmount=1820,fileName="四姑娘山客流.csv").valleyData#300
            
            #读取天气数据
            self.weatherData_si=spider_weather.Weather_spider(place=1,fileName="四姑娘山天气.csv").weatherData

    #合并数据
    def mergeData(self):
        if self.place == 0:
            self.outputData_jiu =pd.merge(self.valleyData_jiu,self.weatherData_jiu,how="left",on="date")
            self.outputData_jiu=self.outputData_jiu.drop('风力',1)
            self.delSymbol("最高温度",self.outputData_jiu)
            self.delSymbol("最低温度",self.outputData_jiu)
            print(self.outputData_jiu)

        elif self.place==1:

            self.outputData_si =pd.merge(self.valleyData_si,self.weatherData_si,how="left",on="date")
            self.outputData_si=self.outputData_si.drop('风力',1)
            self.delSymbol("最高温度",self.outputData_si)
            self.delSymbol("最低温度",self.outputData_si)
            print(self.outputData_si)

    #删除摄氏度
    def delSymbol(self,colName="最高温度",data=None):
        mydata = data
        mydata[colName]=mydata[colName].apply(lambda x: self.processSymbol(x))

    #处理摄氏度
    def processSymbol(self,data_tem):
        try:
            return re.findall("\d+",data_tem)[0]
        except :
            print("小心数据有空值")
            


SpiderModular()